/** bcmi_frame basic util function 
 *
 *  bcmi_frame basic util function
 *
 *  @author : huicong
 *  @date : 2008-10-29
 *
 */

#ifndef _BASIC_UTIL_H
#define _BASIC_UTIL_H

#include <cstdarg>
#include <cstring>

#include "BasicMacro.h"
#include "BasicType.h"
#include "HandledResult.h"
#include "DebugMacro.h"

/** rewrite new, handle error 
 *
 *  new pointer, handle error. only used in no parameter new function
 * 
 *  @param [INOUT] T *_ptr : put an memory space to _ptr
 *  @return HRESULT : new result
 *
 */
template<class T>
inline HRESULT newPointer(INOUT T **_ptr)
{
    HRESULT hr = H_OK;
    try
    {
        (*_ptr) = new T;
    }
    catch (VOID *e)
    {
        hr = H_E_MEMORY_REQUIR_FAILED;
    }
    return hr;
}

/** rewrite new, handle error 
 *
 *  new array, handle error
 * 
 *  @param [INOUT] T *_ptr : put an memory space to _ptr
 *  @param [IN] UINT _dimension : the dimension of  array
 *  @return HRESULT : new result
 *
 */
template<class T>
inline HRESULT newArray(INOUT T **_ptr, IN UINT _dimension)
{
    HRESULT hr = H_OK;
    try
    {
        (*_ptr) = new T[_dimension];
    }
    catch (VOID *e)
    {
        hr = H_E_MEMORY_REQUIR_FAILED;
    }
    return hr;
}

#endif //#ifndef _BASIC_UTIL_H